import { login } from 'api/login'
import { getUserInfo } from 'api/user'
export default {
  namespaced: true,
  state: {
    token: '',
    userInfo: null
  },
  mutations: {
    // 存储token
    setToken (state, playlod) {
      state.token = playlod.token
    },
    // 设置用户信息
    setUserInfo (state, playlod) {
      state.userInfo = playlod.userInfo
    },

    // 清除用户token
    clearToken (state) {
      state.token = ''
    }
  },
  getters: {},
  actions: {
    // 登录
    async onLogin ({ commit }, data) {
      const { data: res } = await login(data)
      if (res.code === 200) {
        commit('setToken', {
          token: res.data.token
        })
        return Promise.resolve(true)
      } else {
        return Promise.reject(new Error(res.message))
      }
    },
    // 获取用户信息
    async onGetUserInfo ({ commit }) {
      const { data: res } = await getUserInfo()
      console.log(res)
      commit('setUserInfo', {
        userInfo: res.data
      })
    }
  }
}
